<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
<HTML>
<HEAD>
<LINK REL=STYLESHEET TYPE="text/css" HREF="../../stylesheet.css" TITLE="Style">
<META NAME="GENERATOR" CONTENT="Java2HTML Version 1.5">
<TITLE>czestmyr.jjsched.jjsched (Java2HTML)</TITLE>
</HEAD>
<BODY><TABLE id="Header" border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td colspan="2" width="33%">&nbsp;</td>
<td align="center" colspan="2" width="33%">
<font size="4">jjsched.java</font>
</td>
<td align="right" colspan="2" width="33%">&nbsp;</td>
</tr>
</TABLE>
<pre ID="Classes">
<A NAME="1"></A><FONT ID="MultiLineComment">/*  This file is a part of JJSched source code
<A NAME="2"></A>    JJSched - Java Jabber Scheduler - a simple Jabber scheduling application
<A NAME="3"></A>    Copyright (C) 2008 Cestmir "CZestmyr" Houska
<A NAME="4"></A>
<A NAME="5"></A>    This program is free software: you can redistribute it and/or modify
<A NAME="6"></A>    it under the terms of the GNU General Public License as published by
<A NAME="7"></A>    the Free Software Foundation, either version 3 of the License, or
<A NAME="8"></A>    (at your option) any later version.
<A NAME="9"></A>
<A NAME="10"></A>    This program is distributed in the hope that it will be useful,
<A NAME="11"></A>    but WITHOUT ANY WARRANTY; without even the implied warranty of
<A NAME="12"></A>    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
<A NAME="13"></A>    GNU General Public License for more details.
<A NAME="14"></A>
<A NAME="15"></A>    You should have received a copy of the GNU General Public License
<A NAME="16"></A>    along with this program.  If not, see &lt;http://www.gnu.org/licenses/&gt; */</FONT>
<A NAME="17"></A>
<A NAME="18"></A><FONT ID="Package">package</FONT> czestmyr.jjsched;
<A NAME="19"></A>
<A NAME="20"></A><FONT ID="Import">import</FONT> java.lang.*;
<A NAME="21"></A><FONT ID="Import">import</FONT> org.jivesoftware.smack.*;
<A NAME="22"></A><FONT ID="Import">import</FONT> org.jivesoftware.smack.packet.*;
<A NAME="23"></A>
<A NAME="24"></A><FONT ID="FormalComment">/**
<A NAME="25"></A> * The main class of the program.
<A NAME="26"></A> * Handles the program initialization, parameter parsing and creation of class instances.
<A NAME="27"></A> */</FONT>
<A NAME="28"></A><FONT ID="Public">public</FONT> <FONT ID="Class">class</FONT> jjsched {
<A NAME="29"></A>    <FONT ID="FormalComment">/** 
<A NAME="30"></A>     * Application entry point.
<A NAME="31"></A>     * Parses the parameters and creates instance of self. Also contains the main program loop.
<A NAME="32"></A>     */</FONT>
<A NAME="33"></A>    <FONT ID="Public">public</FONT> <FONT ID="Static">static</FONT> <FONT ID="Void">void</FONT> main(String[] args){
<A NAME="34"></A>        <FONT ID="SingleLineComment">//Set thread name
<A NAME="35"></A></FONT>        Thread.currentThread().setName(<FONT ID="StringLiteral">"JJSched main thread"</FONT>);
<A NAME="36"></A>        <FONT ID="SingleLineComment">//Set thread priority
<A NAME="37"></A></FONT>        Thread.currentThread().setPriority(<FONT ID="IntegerLiteral">9</FONT>);
<A NAME="38"></A>
<A NAME="39"></A>        <FONT ID="SingleLineComment">//Parse the command line arguments
<A NAME="40"></A></FONT>        <FONT ID="Boolean">boolean</FONT> autocreate = <FONT ID="False">false</FONT>;
<A NAME="41"></A>        String configFile = <FONT ID="StringLiteral">"jjsched.conf"</FONT>;
<A NAME="42"></A>        <FONT ID="For">for</FONT> (<FONT ID="Int">int</FONT> i = <FONT ID="IntegerLiteral">0</FONT>; i &lt; args.length; ++i){
<A NAME="43"></A>            <FONT ID="If">if</FONT> (args[i].equals(<FONT ID="StringLiteral">"--autocreate"</FONT>)) {
<A NAME="44"></A>                autocreate = <FONT ID="True">true</FONT>;
<A NAME="45"></A>            } <FONT ID="Else">else</FONT> <FONT ID="If">if</FONT> (args[i].equals(<FONT ID="StringLiteral">"--configfile"</FONT>)) {
<A NAME="46"></A>                ++i;
<A NAME="47"></A>                <FONT ID="If">if</FONT> (i &gt;= args.length) {
<A NAME="48"></A>                    System.err.println(<FONT ID="StringLiteral">"No configuration file specified after option \"--configfile\""</FONT>);
<A NAME="49"></A>                    <FONT ID="Return">return</FONT>;
<A NAME="50"></A>                }
<A NAME="51"></A>                configFile = args[i];
<A NAME="52"></A>            } <FONT ID="Else">else</FONT> <FONT ID="If">if</FONT> (args[i].equals(<FONT ID="StringLiteral">"--help"</FONT>)) {
<A NAME="53"></A>                System.out.println(<FONT ID="StringLiteral">"JJSched - Java Jabber Scheduler\n"</FONT>+
<A NAME="54"></A>                                   <FONT ID="StringLiteral">"A simple XMPP scheduling application, version 0.1\n"</FONT>+
<A NAME="55"></A>                                   <FONT ID="StringLiteral">"Copyright Cestmir 'Czestmyr' Houska, 2008\n"</FONT>+
<A NAME="56"></A>                                   <FONT ID="StringLiteral">"\n"</FONT>+
<A NAME="57"></A>                                   <FONT ID="StringLiteral">"Command-line options:\n"</FONT>+
<A NAME="58"></A>                                   <FONT ID="StringLiteral">"--configfile file.conf - specifies where the configuration file resides\n"</FONT>+
<A NAME="59"></A>                                   <FONT ID="StringLiteral">"--autocreate           - automatically creates the needed DB tables if they are not created yet\n"</FONT>+
<A NAME="60"></A>                                   <FONT ID="StringLiteral">"                       - Be careful with this option because it can destroy other tables that would have the same name as the jjsched tables"</FONT>);
<A NAME="61"></A>                <FONT ID="Return">return</FONT>;
<A NAME="62"></A>            }
<A NAME="63"></A>        }
<A NAME="64"></A>        <A HREF="../../czestmyr/jjsched/jjsched.java.html">jjsched</A> me = <FONT ID="New">new</FONT> <A HREF="../../czestmyr/jjsched/jjsched.java.html">jjsched</A>(configFile, autocreate);
<A NAME="65"></A>        me.init();
<A NAME="66"></A>        <FONT ID="If">if</FONT> (me.bad) <FONT ID="Return">return</FONT>;
<A NAME="67"></A>
<A NAME="68"></A>        <FONT ID="While">while</FONT> (me.run() &amp;&amp; me.doContinue) {}
<A NAME="69"></A>
<A NAME="70"></A>        me.theConsole.discontinue();
<A NAME="71"></A>    }
<A NAME="72"></A>
<A NAME="73"></A>    <FONT ID="FormalComment">/**
<A NAME="74"></A>     * Constructor.
<A NAME="75"></A>     * Creates instances of JJSched classes and interconnects them. Tries to initialize the connections
<A NAME="76"></A>     * using their respective classes.
<A NAME="77"></A>     */</FONT>
<A NAME="78"></A>    jjsched(String config, <FONT ID="Boolean">boolean</FONT> autocreate){
<A NAME="79"></A>        theMacros = <FONT ID="New">new</FONT> <A HREF="../../czestmyr/jjsched/Macros.java.html">Macros</A>();
<A NAME="80"></A>        <FONT ID="SingleLineComment">//Get jjsched configuration from the config file
<A NAME="81"></A></FONT>        theConfig = <FONT ID="New">new</FONT> <A HREF="../../czestmyr/jjsched/Config.java.html">Config</A>(theMacros);
<A NAME="82"></A>        <FONT ID="Try">try</FONT> {
<A NAME="83"></A>            theConfig.parse(config);
<A NAME="84"></A>        } <FONT ID="Catch">catch</FONT> (Exception e) {
<A NAME="85"></A>            System.out.println(e.getMessage());
<A NAME="86"></A>            bad = <FONT ID="True">true</FONT>;
<A NAME="87"></A>            <FONT ID="Return">return</FONT>;
<A NAME="88"></A>        }
<A NAME="89"></A>
<A NAME="90"></A>        String server = <FONT ID="Null">null</FONT>;
<A NAME="91"></A>        String username = <FONT ID="Null">null</FONT>;
<A NAME="92"></A>        String password = <FONT ID="Null">null</FONT>;
<A NAME="93"></A>        <FONT ID="Try">try</FONT> {
<A NAME="94"></A>            server = theConfig.getString(<FONT ID="StringLiteral">"XMPPServerName"</FONT>);
<A NAME="95"></A>            username = theConfig.getString(<FONT ID="StringLiteral">"XMPPUserName"</FONT>);
<A NAME="96"></A>            password = theConfig.getString(<FONT ID="StringLiteral">"XMPPPassword"</FONT>);
<A NAME="97"></A>        } <FONT ID="Catch">catch</FONT> (Exception e) {
<A NAME="98"></A>            System.out.println(e.getMessage());
<A NAME="99"></A>            bad = <FONT ID="True">true</FONT>;
<A NAME="100"></A>            <FONT ID="Return">return</FONT>;
<A NAME="101"></A>        }
<A NAME="102"></A>
<A NAME="103"></A>        dbc = <FONT ID="New">new</FONT> <A HREF="../../czestmyr/jjsched/DatabaseConnector.java.html">DatabaseConnector</A>(theConfig, theTime);
<A NAME="104"></A>        <FONT ID="Try">try</FONT> {
<A NAME="105"></A>            dbc.connect(autocreate);
<A NAME="106"></A>        } <FONT ID="Catch">catch</FONT> (Exception e) {
<A NAME="107"></A>            System.out.println(<FONT ID="StringLiteral">"Error during MySQL initialization:"</FONT>);
<A NAME="108"></A>            System.out.println(e.getMessage());
<A NAME="109"></A>            bad = <FONT ID="True">true</FONT>;
<A NAME="110"></A>            <FONT ID="Return">return</FONT>;
<A NAME="111"></A>        }
<A NAME="112"></A>
<A NAME="113"></A>        theConnectionConfig = <FONT ID="New">new</FONT> ConnectionConfiguration(server, <FONT ID="IntegerLiteral">5222</FONT>);
<A NAME="114"></A>        theConnectionConfig.setCompressionEnabled(<FONT ID="True">true</FONT>);
<A NAME="115"></A>        theConnectionConfig.setSASLAuthenticationEnabled(<FONT ID="True">true</FONT>);
<A NAME="116"></A>
<A NAME="117"></A>        <FONT ID="Try">try</FONT> {
<A NAME="118"></A>            theConnector = <FONT ID="New">new</FONT> <A HREF="../../czestmyr/jjsched/Connector.java.html">Connector</A>(<FONT ID="New">new</FONT> XMPPConnection(theConnectionConfig), dbc);
<A NAME="119"></A>            theConnector.connect();
<A NAME="120"></A>            theConnector.login(username, password);
<A NAME="121"></A>        } <FONT ID="Catch">catch</FONT> (Exception e) {
<A NAME="122"></A>            <FONT ID="Try">try</FONT> { theConnector.disconnect(); }
<A NAME="123"></A>            <FONT ID="Catch">catch</FONT> (Exception e2) {}
<A NAME="124"></A>            System.out.println(<FONT ID="StringLiteral">"Could not connect to the XMPP server. Check your configuration file: "</FONT> + e.getMessage());
<A NAME="125"></A>            bad = <FONT ID="True">true</FONT>;
<A NAME="126"></A>            <FONT ID="Return">return</FONT>;
<A NAME="127"></A>        }
<A NAME="128"></A>
<A NAME="129"></A>        theParser = <FONT ID="New">new</FONT> <A HREF="../../czestmyr/jjsched/CommandParser.java.html">CommandParser</A>(dbc, theConfig, theMacros, theTime);
<A NAME="130"></A>        theExecutor = <FONT ID="New">new</FONT> <A HREF="../../czestmyr/jjsched/ActionExecutor.java.html">ActionExecutor</A>(theConnector, dbc, theConfig, theTime, <FONT ID="This">this</FONT>);
<A NAME="131"></A>
<A NAME="132"></A>        <FONT ID="SingleLineComment">// Create and run the console
<A NAME="133"></A></FONT>        theConsole = <FONT ID="New">new</FONT> <A HREF="../../czestmyr/jjsched/Console.java.html">Console</A>(theParser, theExecutor, theMacros);
<A NAME="134"></A>        theConsole.start();
<A NAME="135"></A>
<A NAME="136"></A>        theHandler = <FONT ID="New">new</FONT> <A HREF="../../czestmyr/jjsched/PacketHandler.java.html">PacketHandler</A>(theConnector, theConfig, dbc, theTime, theMacros, theParser, theExecutor);
<A NAME="137"></A>        theConnector.addMessageListener(theHandler);
<A NAME="138"></A>        theSender = <FONT ID="New">new</FONT> <A HREF="../../czestmyr/jjsched/Sender.java.html">Sender</A>(dbc, theConnector);
<A NAME="139"></A>    }
<A NAME="140"></A>
<A NAME="141"></A>    <FONT ID="FormalComment">/**
<A NAME="142"></A>     * Initializes the time counters.
<A NAME="143"></A>     */</FONT>
<A NAME="144"></A>    <FONT ID="Private">private</FONT> <FONT ID="Void">void</FONT> init(){
<A NAME="145"></A>        previousSendingTime = System.currentTimeMillis();
<A NAME="146"></A>        nextWaitingTime = <FONT ID="IntegerLiteral">1000</FONT>;
<A NAME="147"></A>    }
<A NAME="148"></A>
<A NAME="149"></A>    <FONT ID="FormalComment">/**
<A NAME="150"></A>     * Runs the program, sending all actual messages in Sender.
<A NAME="151"></A>     *
<A NAME="152"></A>     * @return  True if the program should run on, false otherwise.
<A NAME="153"></A>     */</FONT>
<A NAME="154"></A>    <FONT ID="Private">private</FONT> <FONT ID="Boolean">boolean</FONT> run(){
<A NAME="155"></A>        <FONT ID="SingleLineComment">//Sleep for at most 100 milliseconds
<A NAME="156"></A></FONT>        <FONT ID="Try">try</FONT> {
<A NAME="157"></A>            Thread.currentThread().sleep(nextWaitingTime);
<A NAME="158"></A>        } <FONT ID="Catch">catch</FONT> (InterruptedException e) {}
<A NAME="159"></A>
<A NAME="160"></A>        <FONT ID="SingleLineComment">//Send all actual messages to their receptors
<A NAME="161"></A></FONT>        theSender.sendActual();
<A NAME="162"></A>
<A NAME="163"></A>         <FONT ID="SingleLineComment">//Calculate the new waiting time (100 ms - last execution time)
<A NAME="164"></A></FONT>        nextWaitingTime = <FONT ID="IntegerLiteral">100</FONT> + previousSendingTime - System.currentTimeMillis();
<A NAME="165"></A>        <FONT ID="If">if</FONT> (nextWaitingTime &lt; <FONT ID="IntegerLiteral">0</FONT>) nextWaitingTime = <FONT ID="IntegerLiteral">0</FONT>;
<A NAME="166"></A>        previousSendingTime = System.currentTimeMillis();
<A NAME="167"></A>        <FONT ID="Return">return</FONT> <FONT ID="True">true</FONT>;
<A NAME="168"></A>    }
<A NAME="169"></A>
<A NAME="170"></A>    <FONT ID="FormalComment">/**
<A NAME="171"></A>     * Quit the application
<A NAME="172"></A>     */</FONT>
<A NAME="173"></A>    <FONT ID="Public">public</FONT> <FONT ID="Void">void</FONT> quit() {
<A NAME="174"></A>        doContinue = <FONT ID="False">false</FONT>;
<A NAME="175"></A>    }
<A NAME="176"></A>
<A NAME="177"></A>    <FONT ID="SingleLineComment">//Instances of the program classes
<A NAME="178"></A></FONT>    <FONT ID="Private">private</FONT> ConnectionConfiguration theConnectionConfig;
<A NAME="179"></A>    <FONT ID="Private">private</FONT> <A HREF="../../czestmyr/jjsched/Connector.java.html">Connector</A> theConnector;
<A NAME="180"></A>    <FONT ID="Private">private</FONT> <A HREF="../../czestmyr/jjsched/Config.java.html">Config</A> theConfig;
<A NAME="181"></A>    <FONT ID="Private">private</FONT> <A HREF="../../czestmyr/jjsched/Macros.java.html">Macros</A> theMacros;
<A NAME="182"></A>    <FONT ID="Private">private</FONT> <A HREF="../../czestmyr/jjsched/PacketHandler.java.html">PacketHandler</A> theHandler;
<A NAME="183"></A>    <FONT ID="Private">private</FONT> <A HREF="../../czestmyr/jjsched/DatabaseConnector.java.html">DatabaseConnector</A> dbc;
<A NAME="184"></A>    <FONT ID="Private">private</FONT> <A HREF="../../czestmyr/jjsched/ActionExecutor.java.html">ActionExecutor</A> theExecutor;
<A NAME="185"></A>    <FONT ID="Private">private</FONT> <A HREF="../../czestmyr/jjsched/CommandParser.java.html">CommandParser</A> theParser;
<A NAME="186"></A>    <FONT ID="Private">private</FONT> <A HREF="../../czestmyr/jjsched/TimeHandler.java.html">TimeHandler</A> theTime = <FONT ID="New">new</FONT> <A HREF="../../czestmyr/jjsched/TimeHandler.java.html">TimeHandler</A>();
<A NAME="187"></A>    <FONT ID="Private">private</FONT> <A HREF="../../czestmyr/jjsched/Sender.java.html">Sender</A> theSender;
<A NAME="188"></A>    <FONT ID="Private">private</FONT> <A HREF="../../czestmyr/jjsched/Console.java.html">Console</A> theConsole;
<A NAME="189"></A>
<A NAME="190"></A>    <FONT ID="SingleLineComment">//Some flags
<A NAME="191"></A></FONT>    <FONT ID="Private">private</FONT> <FONT ID="Boolean">boolean</FONT> init = <FONT ID="False">false</FONT>;
<A NAME="192"></A>    <FONT ID="Private">private</FONT> <FONT ID="Boolean">boolean</FONT> bad = <FONT ID="False">false</FONT>;
<A NAME="193"></A>    <FONT ID="Private">private</FONT> <FONT ID="Boolean">boolean</FONT> doContinue = <FONT ID="True">true</FONT>;
<A NAME="194"></A>
<A NAME="195"></A>    <FONT ID="SingleLineComment">//Time counter variables
<A NAME="196"></A></FONT>    <FONT ID="Private">private</FONT> <FONT ID="Long">long</FONT> nextWaitingTime;
<A NAME="197"></A>    <FONT ID="Private">private</FONT> <FONT ID="Long">long</FONT> previousSendingTime;
<A NAME="198"></A>}
<A NAME="199"></A></pre>
</BODY>
</HTML>